package com.howells.time.module.login.view;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.howells.time.R;
import com.howells.time.base.activity.BaseActivity;
import com.howells.time.base.utils.DES;
import com.howells.time.base.utils.SharedPrefsUtil;
import com.howells.time.base.utils.StatusBarUtil;
import com.howells.time.base.utils.ToastShow;
import com.howells.time.module.UIHelper;
import com.howells.time.module.login.presenter.LoginPresenter;
import com.howells.time.module.login.presenter.LoginPresenterImp;
import com.howells.time.module.main.model.User;


public class LoginActivity extends BaseActivity implements LoginView{

    private LoginPresenter loginpresenter;
    private ProgressDialog pd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        loginpresenter=new LoginPresenterImp(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
       /* String user=SharedPrefsUtil.getValue(this,"UserInfo","username",null);
        String psw=SharedPrefsUtil.getValue(this, "UserInfo", "password", null);
        if(user!=null&&psw!=null)
            loginpresenter.login(user,new DES().decrypt(psw));*/
        StatusBarUtil.setColor(this, 0);
        ((TextView)findViewById(R.id.top_pannel_right)).setText("注册");
        ((TextView)findViewById(R.id.top_pannel_title)).setText("");
    }

    @Override
    public void loginSuccess(User info) {
        pd.dismiss();
        UIHelper.jumpMain(this,new Intent());
        this.finish();
        if(info!=null){
            String encode=new DES().encrypt(info.getPassword());
            SharedPrefsUtil.putValue(this,"UserInfo","password",encode);
            SharedPrefsUtil.putValue(this,"UserInfo","username",info.getUsername());
        }
    }

    @Override
    public void loginFail(String msg) {
        ToastShow.ToastShort(this,msg);
        pd.dismiss();
        UIHelper.jumpLogin(this);
    }


    public void login_button(View view) {
        EditText name_edit= (EditText) findViewById(R.id.input_phone_number);
        String name=name_edit.getText().toString();
        if(name.length()==0){
            ToastShow.ToastShort(this,"请输入手机号");
            return;
        }
        EditText password_edit= (EditText) findViewById(R.id.input_password);
        String password=password_edit.getText().toString();
        if(password.length()==0){
            ToastShow.ToastShort(this,"请输入密码");
            return;
        }
        pd=ProgressDialog.show(this,"登录","loading...",false,true);
        loginpresenter.login(name, password);
    }

    public void onClick(View view){
        switch (view.getId()){
            case R.id.top_pannel_back:
                finish();
                break;
            case R.id.top_pannel_right:
                UIHelper.jumpResigister(this);
                break;
        }
    }

    @Override
    public void resultMsg(String msg) {
        pd.dismiss();
        super.resultMsg(msg);
    }

    public void repsw(View view){
        UIHelper.jumpRePsw(this);
    }
}
